package cn.fzc.yipai.task;

import cn.fzc.yipai.entity.Account;
import cn.fzc.yipai.entity.NewGoodsInfo;
import cn.fzc.yipai.entity.StoreInfo;
import cn.fzc.yipai.service.AccountService;
import cn.fzc.yipai.service.GoodsListService;
import cn.fzc.yipai.service.KillService;
import cn.hutool.core.date.DateUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.List;

/**
 * @Author: fu.zc
 * @Date: 2021/7/23 13:57
 * @Description: 商品列表更新task
 */

@Slf4j
@Configuration
@EnableScheduling
public class GoodsKillTask {

    public static final String AM_START_TIME = "10:29:00";
    public static final String AM_END_TIME = "10:32:00";
    public static final String PM_START_TIME = "14:26:00";
    public static final String PM_END_TIME = "14:32:00";
    public static final String NIGHT_START_TIME = "19:06:00";
    public static final String NIGHT_END_TIME = "19:32:00";

    @Autowired
    private GoodsListService goodsListService;
    @Autowired
    private AccountService accountService;
    @Autowired
    private KillService killService;


    //每天更新商品记录
    @Scheduled(cron = "0 0 8,9,10 * * ?")
    //@Scheduled(fixedRate=5000)
    private void configureTasks() {
        log.info("开始更新商品列表");
        goodsListService.updateGoodsList();
    }

    //上午场 10:30 - 11:30
    @SneakyThrows
    @Scheduled(cron = "0 27 10 * * ?")
    private void amKill() {
        StoreInfo storeInfo = StoreInfo.builder().id(1).endTime("11:30:00").build();
        log.info("开始进行抢购游戏!!! ");
        log.info("开始时间:[{}] ", AM_START_TIME);
        log.info("结束时间:[{}] ", AM_END_TIME);
        List<NewGoodsInfo> newGoodsInfos = goodsListService.getDbGoodsList(storeInfo.getId(), DateUtil.today());
        List<Account> loginList = accountService.getLoginList();
        killService.kill(storeInfo, AM_END_TIME, newGoodsInfos, loginList);
    }


    //下午场 14:27 - 15:30
    @SneakyThrows
    @Scheduled(cron = "0 24 14 * * ?")
    private void pmKill() {
        StoreInfo storeInfo = StoreInfo.builder().id(2).endTime("15:30:00").build();
        log.info("开始进行抢购游戏!!! ");
        log.info("开始时间:[{}] ", PM_START_TIME);
        log.info("结束时间:[{}] ", PM_END_TIME);
        List<NewGoodsInfo> newGoodsInfos = goodsListService.getDbGoodsList(storeInfo.getId(), DateUtil.today());
        List<Account> loginList = accountService.getLoginList();
        killService.kill(storeInfo, PM_END_TIME, newGoodsInfos, loginList);
    }

    @SneakyThrows
    @Scheduled(cron = "0 05 19 * * ?")
    private void nightKill() {
        StoreInfo storeInfo = StoreInfo.builder().id(3).endTime("20:10:00").build();
        log.info("开始进行抢购游戏!!! ");
        log.info("开始时间:[{}] ", NIGHT_START_TIME);
        log.info("结束时间:[{}] ", NIGHT_END_TIME);
        List<NewGoodsInfo> newGoodsInfos = goodsListService.getDbGoodsList(storeInfo.getId(), DateUtil.today());
        List<Account> loginList = accountService.getLoginList();
        killService.kill(storeInfo, NIGHT_END_TIME, newGoodsInfos, loginList);
    }


}
